using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIMall : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIMall
        {
            #region ObjectBinding Generate
            public UnityEngine.UI.ExLoopHorizontalScrollRect mallTypeTabLoopList { protected set; get; }
            public UnityEngine.GameObject topLeftBack { protected set; get; }
            public UnityEngine.GameObject mallActivePanel { protected set; get; }
            public UnityEngine.UI.ExLoopVerticalScrollRect mallSubTypeTabLoopList { protected set; get; }
            public UnityEngine.GameObject mallMonthCardPanel { protected set; get; }
            public UnityEngine.GameObject mallRechargePanel { protected set; get; }
            public UnityEngine.GameObject moneyPanel { protected set; get; }
            public UnityEngine.GameObject mallNormalPanel { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopHorizontalScrollRect>("mallTypeTabLoopList", out var __tbv0);
                this.mallTypeTabLoopList = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("topLeftBack", out var __tbv1);
                this.topLeftBack = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("mallActivePanel", out var __tbv2);
                this.mallActivePanel = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("mallSubTypeTabLoopList", out var __tbv3);
                this.mallSubTypeTabLoopList = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("mallMonthCardPanel", out var __tbv4);
                this.mallMonthCardPanel = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("mallRechargePanel", out var __tbv5);
                this.mallRechargePanel = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("moneyPanel", out var __tbv6);
                this.moneyPanel = __tbv6;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("mallNormalPanel", out var __tbv7);
                this.mallNormalPanel = __tbv7;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify


        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIMall),
            layer = EUILayer.Normal,
            isFullScreen = true,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields
        protected UIN_MoneyPanel moneyPanel = null;
        protected LoopScrollAdapter<UIN_MallTypeTab> mallTypeTabs = null;//商城主页签
        protected LoopScrollAdapter<UIN_MallSubTypeTab> mallSubTypeTabs = null;//商城二级页签


        protected List<int> mainTypeList = null;//商城主页签数据
        protected List<int> subTypeCidList = null;//商城二级页签数据
        protected MallOriginalType originalType;

        private int mainTypeIndex = 0;
        private int subTypeIndex = 0;
        private int mallTypeCid = 0;
        private bool isShowSubType = false;
        #endregion fields

        #region properties

        protected UIB_UIMall ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties


        protected override void BeforeInit()
        {
            ui = new UIB_UIMall();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.isUpdate = true;

            this.moneyPanel = this.InitChildNode<UIN_MoneyPanel>(this.ui.moneyPanel);

            this.mallTypeTabs = this.InitLoopScroll<UIN_MallTypeTab>(this.ui.mallTypeTabLoopList, this.OnMainTypeChanged);
            this.mallTypeTabs.InitUnitSelected(UnitSelectType.ByExToggle, this.OnMainTypeSelected);
            this.mallSubTypeTabs = this.InitLoopScroll<UIN_MallSubTypeTab>(this.ui.mallSubTypeTabLoopList, this.OnSubTypeChanged);
            this.mallSubTypeTabs.InitUnitSelected(UnitSelectType.ByExToggle, this.OnSubTypeSelected);

            var topLeftBack = this.InitChildNode<UIN_TopLeftBack>(this.ui.topLeftBack);
            topLeftBack.Show();
        }

        protected override void OnShow(UIBaseData data)
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {
            this.uiMsgDispatcher.AddListener(MsgEventType.MallItemUpdateStartSellTime, this.OnMsgUpdateStartSellTime);
            this.uiMsgDispatcher.AddListener(MsgEventType.MallItemUpdateBuyCountRecords, this.OnMsgUpdateBuyCountRecords);
            this.uiMsgDispatcher.AddListener(MsgEventType.MallItemUpdatePayCount, this.OnMsgUpdatePayCount);
        }

        protected override void OnDispose()
        {
            this.moneyPanel = null;
            this.mallTypeTabs = null;
            this.mallSubTypeTabs = null;
            if (this.mainTypeList != null)
                ListPool<int>.Put(this.mainTypeList);
            this.mainTypeList = null;
            if (this.subTypeCidList != null)
                ListPool<int>.Put(this.subTypeCidList);
            this.subTypeCidList = null;
        }

        #region methonds

        protected void ShowMainTypeView()
        {
            this.mainTypeList.Clear();
            var mainTypes = TableCenter.mallType.GetMallMainTypes(this.originalType);
            int mainType = 0;
            for (int i = 0, cnt = mainTypes.Count; i < cnt; i++)
            {
                mainType = mainTypes[i];
                if (MallModule.Instance.CheckMallTypeIsOpen(this.originalType, mainType))
                {
                    this.mainTypeList.Add(mainType);
                }
            }
            int index = this.mainTypeIndex >= this.mainTypeList.Count ? 0 : this.mainTypeIndex;
            this.mallTypeTabs.RefillCellsAndSelected(this.mainTypeList.Count, index);
        }

        private void OnMainTypeChanged(UIN_MallTypeTab item, int index)
        {
            item.Show(this.originalType, this.mainTypeList[index]);
        }

        private void OnMainTypeSelected(int index, UIN_MallTypeTab item)
        {
            this.mainTypeIndex = index;
            int mainType = this.mainTypeList[index];
            this.ShowSubTypeView(mainType);
        }

        private void ShowSubTypeView(int mainType)
        {
            this.ReInitSubTypeData(mainType);

            int cntSub = this.subTypeCidList.Count;
            bool isShowSubType = true;
            if (this.originalType == MallOriginalType.Mall && mainType != (int)MallNormalMainType.MallNormal)
            {
                isShowSubType = cntSub > 1;
            }
            // isShowSubType = cntSub > 1; // TODO 测试列表子元素消失的BUG逻辑
            this.mallSubTypeTabs.SetActive(isShowSubType);
            if (isShowSubType)
            {
                int index = this.subTypeIndex >= cntSub ? 0 : this.subTypeIndex;
                this.mallSubTypeTabs.RefillCellsAndSelected(cntSub, index);
            }
            else
            {
                this._ShowCurrView(this.subTypeCidList[0], isShowSubType);
            }
        }

        private void ReInitSubTypeData(int mainType)
        {
            this.subTypeCidList.Clear();

            var subTypeCids = TableCenter.mallType.GetMallSubTypeCids(this.originalType, mainType);
            foreach (var mallTypeCid in subTypeCids)
            {
                if (MallModule.Instance.CheckMallTypeIsOpen(mallTypeCid))
                {
                    this.subTypeCidList.Add(mallTypeCid);
                }
            }
        }

        private void OnSubTypeChanged(UIN_MallSubTypeTab item, int index)
        {
            item.Show(this.subTypeCidList[index]);
        }

        private void OnSubTypeSelected(int index, UIN_MallSubTypeTab item)
        {
            this.subTypeIndex = index;
            int mallTypeCid = this.subTypeCidList[index];
            this._ShowCurrView(mallTypeCid, true);
        }

        private void _ShowCurrView(int mallTypeCid, bool isShowSubType)
        {
            this.mallTypeCid = mallTypeCid;
            this.isShowSubType = isShowSubType;
            this.ShowCurrView(this.mallTypeCid, this.isShowSubType);
        }

        protected virtual void ShowCurrView(int mallTypeCid, bool isShowSubType)
        {

        }

        private void RefreshCurrView()
        {
            this.ShowCurrView(this.mallTypeCid, this.isShowSubType);
        }

        protected virtual void OnMsgUpdateStartSellTime()
        {
            this.ShowMainTypeView();
        }

        protected virtual void OnMsgUpdateBuyCountRecords()
        {
            this.RefreshCurrView();
        }

        protected virtual void OnMsgUpdatePayCount()
        {
            this.RefreshCurrView();
        }
        #endregion methonds
    }
}
